// pages/settle/settle.js
var common = require('../../utils/util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderGoods: [],
        address: null,
        quantity: 1,
        account: 0,
        address_id: null,
        loading: true,
        isCar: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            baseUrl: common.baseUrl
        })
        this.handleDefault()
        if (options) {
            //立即购买
            if (options.id) {
                this.initialize(options.id)
                // app.globalData.good_id = options.id
            }
            if (options.quantity) {
                this.setData({
                    quantity: options.quantity
                })
            }

            //购物车购买
            if (options.array) {
                let select = options.array
                let orderGoods = []
                let array = wx.getStorageSync('carList') || []
                for (let y = 0; y < select.length; y++) {
                    for (let i = 0; i < array.length; i++) {
                        if (select[y] == array[i].goods_id) {
                            orderGoods.push(array[i])
                        }
                    }
                }
                this.setData({
                    orderGoods: orderGoods,
                    loading: false,
                    isCar: true
                })
            }
            if (options.price) {
                this.setData({
                    account: options.price
                })
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (this.data.address_id) {
            this.handleAddress(this.data.address_id)
        }
    },

    //立即购买商品信息
    initialize(id) {
        common.http('app/shop/goods/info/' + id, 'GET', (res) => {
            let array = []
            if (res) {
                array.push(res)
                let account = common.accMul(parseFloat(res.price), this.data.quantity)
                this.setData({
                    orderGoods: array,
                    account: account
                })
            }
            this.setData({
                loading: false
            })
        })

    },

    //地址
    handleDefault() {
        common.http('app/user/address/lists', "GET", (res) => {
            if (res.length > 0) {
                this.setData({
                    address: res[0]
                })
            } else {
                common.http('app/user/address/lists', "GET", (res1) => {
                    if (res1) {
                        this.setData({
                            address: res1[0]
                        })
                    }
                })
            }
        }, {
            default: 1
        })
    },
    handleAddress(id) {
        // this.initialize(app.globalData.good_id)
        common.http('app/user/address/info/' + id, "GET", (res) => {
            this.setData({
                address: res
            })
        })
    },
    //选择地址
    handleGoAddress() {
        wx.navigateTo({
            url: '../address/address?title=' + '1',
        })
    },

    handlePay() {
        let data = {
            mode: 3,
            address_id: this.data.address.address_id,
            method: 1,
            scene: 'xcx',
            goods_id: [],
            quantity: []
        }
        let list = this.data.orderGoods
        let that = this
        list.forEach(function (item) {
            data.goods_id.push(item.goods_id)
            if (that.data.isCar) {
                data.quantity.push(item.account)
            } else {
                data.quantity.push(parseInt(that.data.quantity))
            }
        })
        this.setData({
            loading:true
        })
        // data.goods_id.push(app.globalData.good_id)
        common.http('app/order/create', 'POST', (res) => {
            if (res) {
                if (res.pay_params) {
                    let params = res.pay_params
                    params.timeStamp = params.timeStamp + ''
                    if (this.data.isCar){
                        let select = this.data.orderGoods
                        let array = wx.getStorageSync('carList') || []
                        for (let y = 0; y < select.length; y++) {
                            for (let i = 0; i < array.length; i++) {
                                if (select[y].goods_id == array[i].goods_id) {
                                    array.splice(i, 1)
                                }
                            }
                        }
                        var _that = this
                        wx.setStorage({
                            key: "carList",
                            data: array,
                            success() {
                                _that.setData({
                                    carList: array,
                                })
                            }
                        })
                    }
                    var _that = this
                    wx.requestPayment({
                        timeStamp: params.timeStamp,
                        nonceStr: params.nonceStr,
                        package: params.package,
                        signType: 'MD5',
                        paySign: params.paySign,
                        success(res) {
                            common.showToast('支付成功', 'success')
                            wx.switchTab({
                                url: '../index/index',
                            })
                        },
                        fail(res) {
                            common.showToast('支付失败')
                            wx.navigateBack({
                                delta: 1
                            })
                        },
                        complete(res){
                            _that.setData({
                                loading: false,
                            })
                        }
                        // complete(res) {
                        //     if (that.data.isCar) {
                        //         let select = that.orderGoods
                        //         let array = wx.getStorageSync('carList') || []
                        //         for (let y = 0; y < select.length; y++) {
                        //             for (let i = 0; i < array.length; i++) {
                        //                 if (select[y].goods_id == array[i].goods_id) {
                        //                     array.splice(i, 1)
                        //                 }
                        //             }
                        //         }
                        //         var _that = that
                        //         wx.setStorage({
                        //             key: "carList",
                        //             data: array,
                        //             success() {
                        //                 _that.setData({
                        //                     carList: array
                        //                 })
                        //             }
                        //         })
                        //     }
                        // }
                    })
                }

            }
        }, data)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
})